import config from './config'


//接口统一封装
export default function fetch(method, data, callback, show = true) {
    data.method = method;
    post(data, callback, show);
}

// post请求
function post(data, callback, show = true) {
    if (show) {
        wx.showLoading({
            title: '载入中...'
        });
    }
    wx.request({
        url: config.api_url + 'api.html',
        data: data,
        method: 'POST',
        success: function(res) {
            if (show) {
                wx.hideLoading();
            }
            //console.log(res);
            //这里做判断，如果不报错就返回，如果报错，就做错误处理
            if (res.data.status) {
                callback(res.data);
            } else {
                error(res.data, callback);
            }
        },
        fail: function(res) {
            if (show) {
                wx.hideLoading();
            }
            return {
                status: false,
                data: res.data,
                msg: '接口调用失败',
            };
        }
    });
}